package zcj.stu;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.format.number.AbstractNumberFormatter;
import org.springframework.format.number.CurrencyStyleFormatter;
import org.springframework.format.number.money.CurrencyUnitFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.NumberFormat;
import java.util.Locale;
@Configuration
public class WebConfig {

    @ControllerAdvice
    static class myControllerClass{
        @InitBinder
        public void globalBind1(WebDataBinder webDataBinder){
            webDataBinder.addCustomFormatter(new AbstractNumberFormatter() {
                @Override
                protected NumberFormat getNumberFormat(Locale locale) {
                    return NumberFormat.getInstance();
                }
            });
        }
        @InitBinder
        public void globalBind2(WebDataBinder webDataBinder){
            webDataBinder.addCustomFormatter(new CurrencyStyleFormatter());
        }
    }
    @Controller
    @RequestMapping("/c24")
    static class Controller1{
        @InitBinder
        public void controllerBind1(WebDataBinder webDataBinder){
            webDataBinder.addCustomFormatter(new DateFormatter());
        }
        @InitBinder
        public void controllerBind2(WebDataBinder webDataBinder){
            webDataBinder.addCustomFormatter(new CurrencyUnitFormatter());
        }
        @GetMapping("/foo")
        public void foo(){
            System.out.println("我是foo...");
        }
    }

}
